package gui;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import connection.CommandRelay;
import connection.Commands;

public class ControlCanvas extends Canvas implements Runnable {
	private CommandRelay commRelay;
	private int width, height;
	private boolean upPressed = false;
	private int sonarAngle = 90;
	private boolean thrusting;
	private int turn = 0;
	private int vehicleAngle = 90;
	private int[] vehicleXY;
	private Thread thread;
	
	public ControlCanvas(CommandRelay commRelay) {
		this.commRelay = commRelay;
		width = this.getWidth();
		height = this.getHeight();
		vehicleXY = new int[2];
		vehicleXY[0] = width / 2;
		vehicleXY[1] = height / 2;
		thread = new Thread(this, "gui");
		thread.start();
	}
	
	public void run() {
		while (true) {
			this.repaint();
			try {
				//System.out.println("Sleep...");
				Thread.sleep(150);
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void paint(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, width, height);
		
		// Teken de beeldlijnen
		g.setColor(0, 255, 0);
		int dia = width / 5;
		int rad = dia / 2;
		int x = width / 2;
		int y = height / 2;
		int rad2 = rad;
		int dia2 = dia;
		int i = 0;
		while (dia2 < width && dia2 < height) {
			rad2 = i * rad;
			dia2 = dia * i;			
			//System.out.println("width = " + width + " height = " + height + " dia2 = " + dia2 + '\r' + '\n');
			//System.out.println("x - rad2 = " + (x - rad2) + " y - rad2 = " + (y - rad2) + " dia2 = " + dia2);
			g.drawArc(x - rad2, y - rad2, dia2, dia2, 0, 360);
			i++;
		}
		g.drawLine(width / 2, height, width / 2, 0);
		g.drawLine(width, height / 2, 0, height / 2);
		
		// Teken de sonar
		int sonarAngle = this.sonarAngle ;
		for (int j = 0; j < 20; j++) {	
			g.setColor(0,255-i,0);
			drawLine(g, width/2, height/2, sonarAngle, rad2);
			sonarAngle++;
		}
		// Draai de sonar
		if (this.sonarAngle > 360) {
			this.sonarAngle -= 360;
		}
		this.sonarAngle -= 2;
		
		// Teken het voertuig
		drawVessel(g);
		
		
	}
	
	private void drawVessel(Graphics g) {
		if (thrusting) {
			if (vehicleXY[0] < 0) {
				vehicleXY[0] = width;
				if (vehicleXY[1] >= height / 2) {
					vehicleXY[1] = height / 2 - (vehicleXY[1] - height / 2);
				}
				else {
					vehicleXY[1] += height / 2 + (vehicleXY[1] - height / 2);
				}
			}
			else if (vehicleXY[0] > width) {
				vehicleXY[0] = 0;
				if (vehicleXY[1] >= height / 2) {
					vehicleXY[1] = height / 2 - (vehicleXY[1] - height / 2);
				}
				else {
					vehicleXY[1] += height / 2 + (vehicleXY[1] - height / 2);
				}
			}
			if (vehicleXY[1] < 0) {
				vehicleXY[1] = height;
				if (vehicleXY[0] >= width / 2) {
					vehicleXY[0] = width / 2 - (vehicleXY[0] - width / 2);
				}
				else {
					vehicleXY[0] += width / 2 + (vehicleXY[0] - width / 2);
				}
			}
			else if (vehicleXY[1] > height) {
				vehicleXY[1] = 0;
				if (vehicleXY[0] >= width / 2) {
					vehicleXY[0] = width / 2 - (vehicleXY[0] - width / 2);
				}
				else {
					vehicleXY[0] += width / 2 + (vehicleXY[0] - width / 2);
				}
			}
			vehicleXY = drawLine(null, vehicleXY[0], vehicleXY[1], vehicleAngle, 2);
			if (turn != 0) {
				vehicleAngle += turn;
				if (vehicleAngle > 360) { 
					vehicleAngle -= 360; 
				}
				else if (vehicleAngle < 0) {
					vehicleAngle += 360;
				}
			}
		}
		g.setColor(0, 0, 255);
		drawTriangle(g, vehicleXY[0], vehicleXY[1], 10, 20, vehicleAngle);
	}
	
	private int[] drawLine(Graphics g, int x, int y, int angle, int length) {
		int endx = x + new Double(Math.cos(Math.toRadians(angle)) * length).intValue();
		int endy = y - new Double(Math.sin(Math.toRadians(angle)) * length).intValue();
		if (g != null) {
			g.drawLine(x, y, endx, endy);
		}
		return new int[] {endx, endy};
	}
	
	private void drawTriangle(Graphics g, int x, int y, int width, int height, int angle) {
		// bepaal de locatie van de top (vanaf het midden)
		int[] xyTop = drawLine(null, x, y, angle, height/2); 
		// bepaal het middelpunt van de basis (vanaf het midden)
		int[] xyBaseMid = drawLine(null, x, y, 180 + angle, height/2);
		// bepaal het linkerpunt van de basis
		int[] xyBaseLeft = drawLine(null, xyBaseMid[0], xyBaseMid[1], 90 + angle, width/2);
		// bepaal het rechterpunt van de basis
		int[] xyBaseRight = drawLine(null, xyBaseMid[0], xyBaseMid[1], 180 + 90 + angle, width/2);
		
		// Teken de driehoek
		g.drawLine(xyBaseLeft[0], xyBaseLeft[1], xyBaseRight[0], xyBaseRight[1]);
		g.drawLine(xyBaseRight[0], xyBaseRight[1], xyTop[0], xyTop[1]);
		g.drawLine(xyTop[0], xyTop[1], xyBaseLeft[0], xyBaseLeft[1]);
	}
	
	public void keyPressed(int keyCode) {
		switch(getGameAction(keyCode)) {
			case UP:
				if (!upPressed) {
					commRelay.addCommand(Commands.THRUST);
					thrusting = true;
				}
				break;
			case LEFT:
				commRelay.addCommand(Commands.LEFT);
				if (turn < 4) {
					turn += 1;
				}
				break;
			case RIGHT:
				commRelay.addCommand(Commands.RIGHT);
				if (turn > -4) {
					turn -= 1;
				}
				break;
			case KEY_STAR:
				commRelay.addCommand(Commands.STOP);
		}
	}

	public void keyReleased(int keyCode) {
		switch(getGameAction(keyCode)) {
			case UP:
				commRelay.addCommand(Commands.STOPTHRUST);
				thrusting = false;
		}
	}
}
